﻿Imports Persistencia
Imports Relatorio

Public Class frmContatosARealizar

#Region "Variáveis"

    Private WithEvents objRelContatosARealizar As New Relatorio.relContatosARealizar
    Private objClientes As New Controle.ctrCliente
    Private objContato As New Controle.ctrContato
    Private objStatus As New Controle.ctrStatus

#End Region

#Region "Métodos"

    Private Function validaFormulario() As Boolean
        Dim bRetorno As Boolean = True
        Dim sMensagemValidacao As String = ""

        If Not Me.chkTodosClientes.Checked AndAlso Me.txtIDCliente.Text = String.Empty Then
            sMensagemValidacao = "- Informe o cliente."
            bRetorno = False
        End If

        If Not Me.chkTodosContatos.Checked AndAlso Me.txtIDContato.Text = String.Empty Then
            sMensagemValidacao &= Environment.NewLine & "- Informe o contato."
            bRetorno = False
        End If

        If Not Me.chkTodosStatus.Checked AndAlso Me.txtIDStatus.Text = String.Empty Then
            sMensagemValidacao &= Environment.NewLine & "- Informe o status."
            bRetorno = False
        End If

        If sMensagemValidacao <> String.Empty Then
            MsgBox(sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
        End If

        Return bRetorno
    End Function

    Private Sub imprimirRelatorio()
        Dim bRetorno As Boolean

        Try
            If Me.validaFormulario Then
                With Me.objRelContatosARealizar

                    .IDCliente = perConversao.ToInt32(Me.txtIDCliente.Text)
                    .NomeCliente = Me.txtCliente.Text

                    .IDContato = perConversao.ToInt32(Me.txtIDContato.Text)
                    .NomeContato = Me.txtContato.Text

                    .IDStatus = perConversao.ToInt32(Me.txtIDStatus.Text)
                    .NomeStatus = Me.txtStatus.Text

                    .Data = Me.dtpData.Text

                    If Me.cmdImprimir.Focused Then
                        bRetorno = .imprime(relPadrao.eDispositivoSaida.impressora)
                    Else
                        bRetorno = .imprime(relPadrao.eDispositivoSaida.tela)
                    End If


                    If Not bRetorno Then
                        MsgBox("Não existem registros com os dados selecionados", MsgBoxStyle.Information, Me.Text)
                    End If
                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub frmRelClientes_imprimir(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.imprimir
        Me.imprimirRelatorio()
    End Sub

    Private Sub frmRelClientes_visualizar(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.visualizar
        Me.imprimirRelatorio()
    End Sub

    Private Sub frmRelClientes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.lblCliente.Enabled = False
        Me.lblContato.Enabled = False
        Me.lblStatus.Enabled = False

        Me.txtIDCliente.Enabled = False
        Me.txtIDContato.Enabled = False
        Me.txtIDStatus.Enabled = False

        Me.txtCliente.Enabled = False
        Me.txtContato.Enabled = False
        Me.txtStatus.Enabled = False

        Me.cmdProcuraCliente.Enabled = False
        Me.cmdProcuraContato.Enabled = False
        Me.cmdProcuraStatus.Enabled = False

        Me.chkTodosClientes.Checked = True
        Me.chkTodosContatos.Checked = True
        Me.chkTodosStatus.Checked = True

        Me.dtpData.Text = Date.Now

    End Sub

    Private Sub cmdProcuraCliente_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdProcuraCliente.Click
        Dim sql As String
        Dim frmDialogo As New Fronteira.frmPesquisa

        sql = Me.objClientes.SqlConsulta(False)

        With frmDialogo
            .Campos = "iIDCliente,sRazaoSocial"
            .Descricoes = "Código,Nome"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Clientes"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.preencheDescricaoCliente(.Id)
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If
        End With
    End Sub

    Private Sub txtIDCliente_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIDCliente.LostFocus

        Try
            If Me.txtIDCliente.Text <> String.Empty Then
                Me.preencheDescricaoCliente(Me.txtIDCliente.Text)
            Else
                Me.txtCliente.Text = String.Empty
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub preencheDescricaoCliente(ByVal iIdCliente As Int32)

        Dim drDados As DataRow

        drDados = Me.objClientes.selecionarCliente(iIdCliente)

        If Not drDados Is Nothing Then
            Me.txtCliente.Text = drDados.Item("RazaoSocial")
            Me.txtIDCliente.Text = drDados.Item("IDCliente")
        Else
            MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
            Me.txtIDCliente.Text = String.Empty
            Me.txtIDCliente.Focus()
            Exit Sub
        End If

    End Sub

    Private Sub cmdProcuraContato_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdProcuraContato.Click
        Dim sql As String
        Dim frmDialogo As New Fronteira.frmPesquisa

        sql = Me.objContato.sqlConsulta(perConversao.ToInt32(Me.txtIDCliente.Text))

        With frmDialogo
            .Campos = "iIDContato,sNomeContato"
            .Descricoes = "Código,Descrição"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Contatos"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.preencheDescricaoContato(.Id)
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If
        End With
    End Sub

    Private Sub txtIDContato_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIDContato.LostFocus

        Try
            If Me.txtIDContato.Text <> String.Empty Then
                Me.preencheDescricaoContato(Me.txtIDContato.Text)
            Else
                Me.txtContato.Text = String.Empty
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub preencheDescricaoContato(ByVal iIdContato As Int32)

        Dim dsDados As New DataSet

        dsDados = Me.objContato.selecionarContato(iIdContato, perConversao.ToInt32(Me.txtIDCliente.Text), False)

        If Not dsDados Is Nothing AndAlso dsDados.Tables(0).Rows.Count > 0 Then
            Me.txtContato.Text = dsDados.Tables(0).Rows(0).Item("NomeContato")
            Me.txtIDContato.Text = dsDados.Tables(0).Rows(0).Item("IDContato")
        Else
            MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
            Me.txtIDContato.Text = String.Empty
            Me.txtIDContato.Focus()
            Exit Sub
        End If

    End Sub

    Private Sub cmdProcuraStatus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdProcuraStatus.Click
        Dim sql As String
        Dim frmDialogo As New Fronteira.frmPesquisa

        sql = Me.objStatus.sqlConsulta()

        With frmDialogo
            .Campos = "iIDStatus,sDescricao"
            .Descricoes = "Código,Descrição"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Status"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.preencheDescricaoStatus(.Id)
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If
        End With
    End Sub

    Private Sub txtIDStatus_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIDStatus.LostFocus

        Try
            If Me.txtIDStatus.Text <> String.Empty Then
                Me.preencheDescricaoStatus(Me.txtIDStatus.Text)
            Else
                Me.txtStatus.Text = String.Empty
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub preencheDescricaoStatus(ByVal iIdStatus As Int32)

        Dim drDados As DataRow

        drDados = Me.objStatus.selecionarStatus(iIdStatus)

        If Not drDados Is Nothing Then
            Me.txtStatus.Text = drDados.Item("Descricao")
            Me.txtIDStatus.Text = drDados.Item("IDStatus")
        Else
            MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
            Me.txtIDStatus.Text = String.Empty
            Me.txtIDStatus.Focus()
            Exit Sub
        End If

    End Sub

    Private Sub chkTodosClientes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTodosClientes.CheckedChanged
        If sender.checked Then
            Me.lblCliente.Enabled = False
            Me.txtIDCliente.Enabled = False
            Me.txtCliente.Enabled = False
            Me.cmdProcuraCliente.Enabled = False
            Me.txtIDCliente.Focus()
            Me.txtIDCliente.Text = String.Empty
            Me.txtCliente.Text = String.Empty
        Else
            Me.lblCliente.Enabled = True
            Me.txtIDCliente.Enabled = True
            Me.txtCliente.Enabled = True
            Me.cmdProcuraCliente.Enabled = True
        End If
    End Sub

    Private Sub chkTodasRegioes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTodosContatos.CheckedChanged
        If sender.checked Then
            Me.lblContato.Enabled = False
            Me.txtIDContato.Enabled = False
            Me.txtContato.Enabled = False
            Me.cmdProcuraContato.Enabled = False
            Me.txtIDContato.Focus()
            Me.txtIDContato.Text = String.Empty
            Me.txtContato.Text = String.Empty
        Else
            Me.lblContato.Enabled = True
            Me.txtIDContato.Enabled = True
            Me.txtContato.Enabled = True
            Me.cmdProcuraContato.Enabled = True
        End If
    End Sub

    Private Sub chkTodosStatus_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTodosStatus.CheckedChanged
        If sender.checked Then
            Me.lblStatus.Enabled = False
            Me.txtIDStatus.Enabled = False
            Me.txtStatus.Enabled = False
            Me.cmdProcuraStatus.Enabled = False
            Me.txtIDStatus.Focus()
            Me.txtIDStatus.Text = String.Empty
            Me.txtStatus.Text = String.Empty
        Else
            Me.lblStatus.Enabled = True
            Me.txtIDStatus.Enabled = True
            Me.txtStatus.Enabled = True
            Me.cmdProcuraStatus.Enabled = True
        End If
    End Sub

#End Region

   
End Class
